import { _decorator, BoxCollider2D, Collider, Component, Contact2DType, EventTouch, Input, input, IPhysics2DContact, ITriggerEvent, Node, RigidBody2D, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BallController')
export class BallController extends Component {
	@property
	public minPosx: number = 0

	@property
	public maxPosX: number = 0

	private _rigidBody: RigidBody2D | null = null

	private _offset: Vec3 = new Vec3()

	private _ballCollider: BoxCollider2D | null = null
	private _contactCards: Node[] = []


	start() {
		input.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
		input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)

		input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
		this._rigidBody = this.node.getComponent(RigidBody2D)
		if (this._rigidBody) {
			this._rigidBody.enabled = false
		}

		this._ballCollider = this.node.getComponent(BoxCollider2D)
		if (!this._ballCollider) {
			this._ballCollider = this.node.addComponent(BoxCollider2D)
        }
        this._ballCollider.on(
						Contact2DType.BEGIN_CONTACT,
						this.onBeginContact,
						this
					)
		

		this.schedule(this.checkRigidBodySleep, 0.1)
	}

	onBeginContact(selfCollider: Collider, otherCollider: Collider, contact: IPhysics2DContact | null) {
		
		if (otherCollider.node.name.includes('Card')) {
			
				 this._contactCards.push(otherCollider.node)
			}
       
    }
 

    

	checkRigidBodySleep() {
		if (this._rigidBody && !this._rigidBody.isAwake()) {
			this.onBallSleep()
		}
	}
	onBallSleep() {
        this.unschedule(this.checkRigidBodySleep)
        // 获取this.__contactCards数组中最后一个元素
        const lastCard = this._contactCards.pop();
        if (lastCard) {
          const nodeName = lastCard.name;
					console.log("nodeName: " + nodeName);		
				}
		
				this.onGameOver(lastCard)
        
	}

	onTouchStart(event: EventTouch) {
		const touchPos = event.getUILocation()
		const curPos = this.node.position
		Vec3.subtract(this._offset, new Vec3(touchPos.x, 0), new Vec3(curPos.x, 0))
	}

	onTouchMove(event: EventTouch) {
		const touchPos = event.getUILocation()
		const curPos = this.node.position
		const newPos = new Vec3()
		Vec3.subtract(newPos, new Vec3(touchPos.x, 0), this._offset)
		const posX = Math.max(this.minPosx, Math.min(newPos.x, this.maxPosX))
    this.node.setPosition(new Vec3(posX, curPos.y, curPos.z))
        
	}


	onTouchEnd() {
		console.log('onTouchEnd')
		if (this._rigidBody) {
			this._rigidBody.enabled = true
        }
    const force = Math.random() * 1000
		this._rigidBody.applyForceToCenter(new Vec2(0, -force), true)
		input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
    }
    
    onGameOver(lastCard:Node) {
			this.node.emit("gameOver", lastCard);
    }

	update(deltaTime: number) {}

	protected onDestroy(): void {
		input.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
		input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
		this._ballCollider.off(
						Contact2DType.BEGIN_CONTACT,
						this.onBeginContact,
						this
					);

	}
}


